package cz.muni.fi.pa165.bookingmanager.interceptors;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

public class LoggingInterceptor {

    private static final Logger logger = Logger.getLogger(LoggingInterceptor.class.getName());

    @AroundInvoke
    public Object logMethodCall(InvocationContext invocationContext) throws Exception {
        String method = invocationContext.getMethod().getName();

        List<Object> parameters = new ArrayList<Object>();

        Object[] objects = invocationContext.getParameters();

        if (objects == null) {
            parameters.add("default");
        } else {
            for (Object o : objects) {
                if (o != null) {
                    parameters.add(o);
                }
            }
        }
        logger.log(Level.INFO, "Method :{0} called with parameters {1}.", new Object[]{method, parameters.toString()});

        return invocationContext.proceed();
    }
}
